package combat5;

import java.util.ArrayList;
import java.util.Scanner;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.*;
//import demo.grid.MyGridPackage.GridException;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import ca.etsmtl.log720.lab1.BanqueDossiers;
import ca.etsmtl.log720.lab1.BanqueDossiersHelper;
import ca.etsmtl.log720.lab1.BanqueInfractions;
import ca.etsmtl.log720.lab1.BanqueInfractionsHelper;
import ca.etsmtl.log720.lab1.BanqueReactions;
import ca.etsmtl.log720.lab1.BanqueReactionsHelper;
import ca.etsmtl.log720.lab1.CollectionDossier;
import ca.etsmtl.log720.lab1.CollectionInfraction;
import ca.etsmtl.log720.lab1.CollectionReaction;
import ca.etsmtl.log720.lab1.Dossier;
import ca.etsmtl.log720.lab1.Infraction;
import ca.etsmtl.log720.lab1.InvalidIdException;
import ca.etsmtl.log720.lab1.Reaction;
//create by martin souliere
public class ClientVoiture {
	public static Dossier currentDossier;
	public static String _module;

	public static void main(String args[]) throws InvalidName, NotFound,
			CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName {

		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
		// get hold of the naming service
		NamingContextExt nc = NamingContextExtHelper.narrow(orb
				.resolve_initial_references("NameService"));

		Scanner inputReader = new Scanner(System.in);
		System.out.println("Please enter input from command prompt: ");
		while (true) {
			showOptions();

			int number;
			try {
				number = inputReader.nextInt();
			} catch (Exception e) {
				System.out.println("Valeur invalide");
				number = 0;
			}

			if (number == 0)
				break;
			switch (number) {
			case 1:
				selectionDossier(inputReader, nc);
				break;
			case 2:
				ajoutReation(inputReader, nc);
				break;
			case 3:
				ajoutReationDossier(inputReader, nc);
				break;
			case 4:
				ajoutInfractionDossier(inputReader, nc);
				break;
			case 5:
				visualiserDossier(nc);
				break;
			case 6:
				visualiserInfractions(nc);
				break;
			case 7:
				visualiserReaction(nc);
				break;
			default:
				System.out.println("choix Invalide");
				break;
			}
		}
	}

	private static void showOptions() {
		System.out.println("0-Fermer l'application");
		System.out.println("1-Selection dossiers");
		System.out.println("2-Ajouter une reaction");
		System.out.println("3-Ajouter une reaction au Dossier selectionnee");
		System.out.println("4-Ajouter une infractions au Dossier selectionnee");
		System.out.println("5-Visualiser Dossier selectionnee");
		System.out.println("6-Visualiser les infractions");
		System.out.println("7-Visualiser les reactions");
	}

	private static void selectionDossier(Scanner inputReader,
			NamingContextExt nc) throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueDossiers = new NameComponent[] { new NameComponent(
				"BanqueDossiers", "service") };
		// resolve name to get a reference to our server
		BanqueDossiers banqueDossiers = BanqueDossiersHelper.narrow(nc
				.resolve(nameBanqueDossiers));
		CollectionDossier dossiers = null;
		System.out.println("1-Par nom complet");
		System.out.println("2-Par Numero de plaque");
		System.out.println("3-Par Numero de permis");
		int number = 0;
		try {
			number = inputReader.nextInt();
		} catch (Exception e) {

		}

		switch (number) {
		case 1:
			System.out.println("Veuillez entrer un nom");
			String nom = inputReader.next();
			System.out.println("Veuillez entrer un prenom");
			String prenom = inputReader.next();
			dossiers = banqueDossiers.trouverDossiersParNom(nom, prenom);
			choixDossier(inputReader, dossiers);
			break;
		case 2:
			System.out.println("Veuillez entrer un numero de plaque");
			String plaque = inputReader.next();
			dossiers = banqueDossiers.trouverDossiersParPlaque(plaque);
			choixDossier(inputReader, dossiers);
			break;
		case 3:
			System.out.println("Veuillez entrer un numero de permis");
			String noPermis = inputReader.next();
			Dossier dossier = banqueDossiers.trouverDossierParPermis(noPermis);
			currentDossier = dossier;
			if (dossier == null)
				System.out.println("Aucun dossier trouve");

			break;
		default:
			System.out.println("Choix invalid");
			break;
		}
	}

	private static void choixDossier(Scanner inputReader,
			CollectionDossier dossiers) {
		if (dossiers.size() == 0) {
			System.out.println("Aucun dossier trouve");
			currentDossier = null;
			return;
		}
		for (int i = 0; i < dossiers.size(); i++) {
			System.out
					.println(i + 1 + "-" + dossiers.getDossier(i)._toString());
		}
		System.out.println("Veuillez entrer votre choix");
		int choix = 0;
		try {
			choix = inputReader.nextInt();
		} catch (Exception e) {
		}

		if (choix < 1 || choix > dossiers.size()) {
			System.out.println("Choix invalid");
			return;
		}
		currentDossier = dossiers.getDossier(choix - 1);
	}

	private static void ajoutReation(Scanner inputReader, NamingContextExt nc)
			throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueReactions = new NameComponent[] { new NameComponent(
				"BanqueReactions", "module1") };
		// resolve name to get a reference to our server
		BanqueReactions banqueReactions = BanqueReactionsHelper.narrow(nc
				.resolve(nameBanqueReactions));

		System.out.println("Veuillez entrer la reaction:");
		String reaction = inputReader.next();
		System.out.println("Veuillez entrer la gravite entre 1 et 10:");

		int gravite = 0;
		try {
			gravite = inputReader.nextInt();
		} catch (Exception e) {
		}

		while (true) {
			if (gravite < 1 || gravite > 10) {
				System.out.println("la gravite doit etre entre 1 et 10");
				System.out.println("Veuillez entrer une gravite a nouveau:");
				System.out.println("Pour annuler faite le 0");
				gravite = 0;
				try {
					gravite = inputReader.nextInt();
				} catch (Exception e) {
				}

				if (gravite == 0) {
					break;
				}
			} else {
				banqueReactions.ajouterReaction(reaction, gravite);
				break;
			}
		}
	}

	private static void ajoutReationDossier(Scanner inputReader,
			NamingContextExt nc) throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		if (currentDossier == null) {
			System.out.println("Vous devez selectionne un dossier");
			return;
		}
		NameComponent[] nameBanqueReactions = new NameComponent[] { new NameComponent(
				"BanqueReactions", "module1") };
		// resolve name to get a reference to our server
		BanqueReactions banqueReactions = BanqueReactionsHelper.narrow(nc
				.resolve(nameBanqueReactions));
		Reaction reaction = choixReaction(inputReader,
				banqueReactions.reactions());
		if (reaction == null) {
			return;
		}
		NameComponent[] nameBanqueDossiers = new NameComponent[] { new NameComponent(
				"BanqueDossiers", "service") };
		// resolve name to get a reference to our server
		BanqueDossiers banqueDossiers = BanqueDossiersHelper.narrow(nc
				.resolve(nameBanqueDossiers));
		try {
			banqueDossiers.ajouterReactionAuDossier(currentDossier.id(),
					reaction.id());
		} catch (InvalidIdException e) {
			System.out.println("Dossier introuvable");
		}
	}

	private static Reaction choixReaction(Scanner inputReader,
			CollectionReaction reactions) {
		if (reactions.size() == 0) {
			System.out.println("Aucune reaction existante");
			return null;
		}
		for (int i = 0; i < reactions.size(); i++) {
			System.out.println(i + 1 + "-"
					+ reactions.getReaction(i)._toString());
		}
		System.out.println("Veuillez entrer votre choix");
		int choix = 0;
		try {
			choix = inputReader.nextInt();
		} catch (Exception e) {
			choix = 0;
		}

		if (choix < 1 || choix > reactions.size()) {
			System.out.println("Choix invalid");
			return null;
		}
		return reactions.getReaction(choix - 1);

	}

	private static void ajoutInfractionDossier(Scanner inputReader,
			NamingContextExt nc) throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		if (currentDossier == null) {
			System.out.println("Vous devez selectionne un dossier");
			return;
		}
		NameComponent[] nameBanqueInfractions = new NameComponent[] { new NameComponent(
				"BanqueInfractions", "service") };
		BanqueInfractions banqueInfractions = BanqueInfractionsHelper.narrow(nc
				.resolve(nameBanqueInfractions));
		Infraction infraction = choixInfraction(inputReader,
				banqueInfractions.infractions());
		if (infraction == null) {
			return;
		}
		NameComponent[] nameBanqueDossiers = new NameComponent[] { new NameComponent(
				"BanqueDossiers", "service") };
		// resolve name to get a reference to our server
		BanqueDossiers banqueDossiers = BanqueDossiersHelper.narrow(nc
				.resolve(nameBanqueDossiers));
		try {
			banqueDossiers.ajouterInfractionAuDossier(currentDossier.id(),
					infraction.id());
		} catch (InvalidIdException e) {
			System.out.println("Dossier introuvable");
		}

	}

	private static Infraction choixInfraction(Scanner inputReader,
			CollectionInfraction infractions) {
		if (infractions.size() == 0) {
			System.out.println("Aucune reaction existante");
			return null;
		}
		for (int i = 0; i < infractions.size(); i++) {
			System.out.println(i + 1 + "-"
					+ infractions.getInfraction(i)._toString());
		}
		System.out.println("Veuillez entrer votre choix");
		int choix = 0;
		try {
			choix = inputReader.nextInt();
		} catch (Exception e) {
		}

		if (choix < 1 || choix > infractions.size()) {
			System.out.println("Choix invalid");
			return null;
		}
		return infractions.getInfraction(choix - 1);
	}

	private static void visualiserDossier(NamingContextExt nc) throws NotFound,
			CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName {
		if (currentDossier == null)
			System.out.println("Vous devez selectionne un dossier");
		else {
			System.out.println(currentDossier._toString());
			NameComponent[] nameBanqueInfractions = new NameComponent[] { new NameComponent(
					"BanqueInfractions", "service") };
			BanqueInfractions banqueInfractions = BanqueInfractionsHelper.narrow(nc
					.resolve(nameBanqueInfractions));
			CollectionInfraction colInfraction = banqueInfractions.infractions();
				System.out.println("Infactions");
				System.out.println(currentDossier.getListeInfraction().length);
				for (int i = 0; i < currentDossier.getListeInfraction().length; i++) {
					for (int j = 0; j < colInfraction.size(); j++) {
						if (currentDossier.getListeInfraction()[i] == colInfraction
								.getInfraction(j).id()) {
							System.out.println(colInfraction.getInfraction(j)
									._toString());
							break;
						}
					}
				}
			NameComponent[] nameBanqueReactions = new NameComponent[] { new NameComponent(
					"BanqueReactions", "module1") };
			// resolve name to get a reference to our server
			BanqueReactions banqueReactions = BanqueReactionsHelper.narrow(nc
					.resolve(nameBanqueReactions));
			CollectionReaction reactions = banqueReactions.reactions();	
			System.out.println(currentDossier.getListeInfraction().length);
				System.out.println("Reactions");
				System.out.println(currentDossier.getListeReaction().length);
				for (int i = 0; i < currentDossier.getListeReaction().length; i++) {
					for (int j = 0; j < reactions.size(); j++) {
						if (currentDossier.getListeReaction()[i] == reactions
								.getReaction(j).id()) {
							System.out.println(reactions.getReaction(j)
									._toString());
							break;
						}
					}
				}
			}
	}

	private static void visualiserInfractions(NamingContextExt nc)
			throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueInfractions = new NameComponent[] { new NameComponent(
				"BanqueInfractions", "service") };
		BanqueInfractions banqueInfractions = BanqueInfractionsHelper.narrow(nc
				.resolve(nameBanqueInfractions));
		CollectionInfraction infractions = banqueInfractions.infractions();
		for (int i = 0; i < infractions.size(); i++) {
			System.out.println(infractions.getInfraction(i)._toString());
		}
	}

	private static void visualiserReaction(NamingContextExt nc)
			throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueReactions = new NameComponent[] { new NameComponent(
				"BanqueReactions", "module1") };
		BanqueReactions banqueReactions = BanqueReactionsHelper.narrow(nc
				.resolve(nameBanqueReactions));
		CollectionReaction reactions = banqueReactions.reactions();
		for (int i = 0; i < reactions.size(); i++) {
			System.out.println(reactions.getReaction(i)._toString());
		}
	}
}
